const path = require('path');
const { name } = require('./package');
const CopyPlugin = require('copy-webpack-plugin');

function resolve(dir) {
    return path.join(__dirname, dir);
}

const dev = process.env.NODE_ENV === 'development';
const port = 9001; // dev port

module.exports = {
    lintOnSave: false,
    publicPath: dev ? `//localhost:${port}` : './',
    outputDir: 'dist',
    assetsDir: 'static',
    filenameHashing: true,
    productionSourceMap: false,

    chainWebpack: config => {
        if (process.env.NODE_ENV === 'prod') {
            config.output
                .filename('static/js/[name].[hash].js')
                .chunkFilename('static/js/[name].[hash].js')
                .end();
        }
        config.module.rules.delete('svg');
        config.module
            .rule('svg-smart')
            .test(/\.svg$/)
            .include.add(resolve('src/icons/svg'))
            .end()
            .use('svg-sprite-loader')
            .loader('svg-sprite-loader')
            .options({
                symbolId: '[name]',
            });
        config.module
            .rule('fonts')
            .test(/.(ttf|otf|eot|woff|woff2)$/)
            .use('url-loader')
            .loader('url-loader')
            .tap(options => {
                options = {
                    // limit: 10000,
                    name: '/static/fonts/[name].[ext]',
                };
                return options;
            });
    },

    devServer: {
        hot: true,
        historyApiFallback: true,
        port: port,
        open: true,
        overlay: {
            warnings: false,
            errors: true,
        },
        headers: {
            'Access-Control-Allow-Origin': '*',
        },
        proxy: {
            '/proxyApi': {
                target: 'http://192.168.1.88:8088',
                changeOrigin: true, // 是否跨域
                ws: false, // 是否支持 websocket
                secure: false, // 如果是 https 接口，需要配置这个参数
                pathRewrite: {
                    '^/proxyApi': '',
                },
            },
        },
    },

    // 自定义webpack配置
    configureWebpack: config => {
        if (process.env.NODE_ENV !== 'prod') {
            // 开发环境配置(方便debug)
            config.devtool = 'source-map';
        }
        return {
            resolve: {
                alias: {
                    '@': resolve('src'),
                },
            },
            output: {
                library: `${name}`,
                libraryTarget: 'umd',
                jsonpFunction: `webpackJsonp_${name}`,
                globalObject: 'window',
            },
            plugins: [
                new CopyPlugin({
                    patterns: [
                        {
                            from: path.resolve(
                                __dirname,
                                `./config/index${
                                    process.env.NODE_ENV !== 'development'
                                        ? '-' + process.env.NODE_ENV
                                        : ''
                                }.js`
                            ),
                            to: './config/index.js',
                        },
                    ],
                }),
            ],
        };
    },
};
